package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ulwx.database.DbException;
import com.ulwx.tool.DbUtils;
import com.ulwx.type.TResult;

import feeinterface.modeldb.Mobile;

public class MobileDao {
	private static final Logger log = Logger.getLogger(MobileDao.class);

	/**
	 * 
	 * @param m
	 * @return true：老用户，false：新用户
	 * @throws Exception
	 */
	public static boolean registIfnew(Mobile m,TResult<Mobile> oldMoile) throws Exception {

		try {
			Mobile old = MobileDao.getByIMSI(m.getIMSI());
			if (old == null) {
				int i = DbUtils.excuteInsertClass(null, m);
				oldMoile.setValue(m);
				return false;// 表明是新注册用户

			} else {
				oldMoile.setValue(old);
				return true;
			}
		} catch (Exception e) {
			log.error("", e);
			throw e;
		}

	}

	public static void insert(Mobile m) throws Exception {
		DbUtils.excuteInsertClass(null, m);
	}

	/**
	 * 通过IMSI获取手机扩展记录
	 * 
	 * @param IMSI
	 * @return
	 * @throws Exception
	 */
	public static Mobile getByIMSI(String IMSI) throws Exception {
		String sql = "select * from Mobile where IMSI=?";
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, IMSI);
		List<Mobile> list = DbUtils.doQueryClass(null, Mobile.class, sql, map);
		if (list.size() > 0)
			return list.get(0);
		return null;
	}
	
	public static void updateGetPhoneCnt(String imsi){
		String sql="update MobileExt set MonthGetPhoneCnt=MonthGetPhoneCnt+1," +
				"DayGetPhoneCnt=DayGetPhoneCnt+1 where IMSI=?";
		try{
			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(1, imsi);
			DbUtils.executeBindUpdate(null, sql, map);
		}catch(Exception e){
			log.error("",e);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
